// var i;
// var $eventManager = require("./EventManager");
// var $resKeeper = require("./ResKeeper");
// var $timeUtils = require("./TimeUtils");
// var $evenType = require("./EvenType");
// var $playerDataManager = require("./PlayerDataManager");
// var $gameGlobalVariable = require("./GameGlobalVariable");
// var h = cc._decorator;
// var f = h.ccclass;
// var g = h.property;
// var _ =
//     (h.inspector,
//     (function (e) {
//         function t() {
//             var t = (null !== e && e.apply(this, arguments)) || this;
//             t.timeLabel = null;
//             return t;
//         }
//         __extends(t, e);
//         t.prototype.onEnable = function () {
//             this.ResetShowTime();
//             $eventManager.EventMgr.addEventListener($evenType.EVENT_TYPE.Hall_ResetRich, this.ResetShowTime, this);
//         };
//         t.prototype.onDisable = function () {
//             $eventManager.EventMgr.rmEventListener($evenType.EVENT_TYPE.Hall_ResetRich, this.ResetShowTime, this);
//         };
//         t.prototype.ResetShowTime = function () {
//             var e = this;
//             this.timeLabel.node.stopAllActions();
//             this.timeLabel.string = "";
//             if (
//                 $playerDataManager.PlayerDataMgr.globalData.power < $playerDataManager.PlayerDataMgr.globalData.maxPower
//             ) {
//                 var t = new Date().getTime();
//                 var o = 6e4 * $gameGlobalVariable.GameGlobalVariable.addPowerTime;
//                 var n = t - $playerDataManager.PlayerDataMgr.globalData.lastAddPowerTime;
//                 if (n >= o) {
//                     var i = Math.floor(n / o);
//                     if (
//                         $playerDataManager.PlayerDataMgr.globalData.power + i >
//                         $playerDataManager.PlayerDataMgr.globalData.maxPower
//                     ) {
//                         return void $playerDataManager.PlayerDataMgr.AddPower(
//                             $playerDataManager.PlayerDataMgr.globalData.maxPower -
//                                 $playerDataManager.PlayerDataMgr.globalData.power
//                         );
//                     }
//                     $playerDataManager.PlayerDataMgr.AddPower(i);
//                     n -= i * o;
//                     $playerDataManager.PlayerDataMgr.globalData.lastAddPowerTime = t - n;
//                 }
//                 n = o - n;
//                 n = Math.floor(0.001 * n);
//                 this.timeLabel.string = $timeUtils.default.secondsToMinutesSeconds(n);
//                 this.timeLabel.node.runAction(
//                     cc.repeatForever(
//                         cc.sequence(
//                             cc.delayTime(1),
//                             cc.callFunc(function () {
//                                 n--;
//                                 e.timeLabel.string = $timeUtils.default.secondsToMinutesSeconds(n);
//                                 if (0 == n) {
//                                     e.ResetShowTime();
//                                 }
//                             })
//                         )
//                     )
//                 );
//             }
//         };
//         __decorate([g(cc.Label)], t.prototype, "timeLabel", void 0);
//         return __decorate([f], t);
//     })($resKeeper.default));
// exports.default = _;

import { EventMgrInstance } from "./EventManager";
import ResKeeper from "./ResKeeper";
import TimeUtils from "./TimeUtils";
import { EVENT_TYPE } from "./EvenType";
import { PlayerDataMgr } from "./PlayerDataManager";
import { GameGlobalVariable } from "./GameGlobalVariable";

const { ccclass, property } = cc._decorator;

/**
 * 建议类名: TimeLabelUpdater
 */
@ccclass
class AddPowerByTime extends ResKeeper {
    @property(cc.Label)
    public timeLabel: cc.Label | null = null;

    /**
     * 当组件启用时调用，注册事件监听器
     * 建议方法名: onComponentEnable
     */
    onEnable(): void {
        this.resetDisplayTime();
        EventMgrInstance.addListener(EVENT_TYPE.Hall_ResetRich, this.resetDisplayTime, this);
    }

    /**
     * 当组件禁用时调用，移除事件监听器
     * 建议方法名: onComponentDisable
     */
    onDisable(): void {
        EventMgrInstance.removeListener(EVENT_TYPE.Hall_ResetRich, this.resetDisplayTime, this);
    }

    /**
     * 重置显示时间
     * 建议方法名: resetDisplayTime
     */
    resetDisplayTime(): void {
        if (!this.timeLabel) return;

        this.timeLabel.node.stopAllActions();
        this.timeLabel.string = "";

        const playerGlobalData = PlayerDataMgr.globalData;

        if (playerGlobalData.power < playerGlobalData.maxPower) {
            const currentTimeMs = new Date().getTime();
            const addPowerIntervalMs = 60000 * GameGlobalVariable.addPowerTime;
            let elapsedAddPowerTime = currentTimeMs - playerGlobalData.lastAddPowerTime;

            if (elapsedAddPowerTime >= addPowerIntervalMs) {
                const powerToAddCount = Math.floor(elapsedAddPowerTime / addPowerIntervalMs);
                if (playerGlobalData.power + powerToAddCount > playerGlobalData.maxPower) {
                    PlayerDataMgr.addPower(playerGlobalData.maxPower - playerGlobalData.power);
                    return;
                }

                PlayerDataMgr.addPower(powerToAddCount);
                elapsedAddPowerTime -= powerToAddCount * addPowerIntervalMs;
                playerGlobalData.lastAddPowerTime = currentTimeMs - elapsedAddPowerTime;
            }

            elapsedAddPowerTime = addPowerIntervalMs - elapsedAddPowerTime;
            let remainingTimeSeconds = Math.floor(elapsedAddPowerTime * 0.001);
            this.timeLabel.string = TimeUtils.convertSecondsToMinutesSeconds(remainingTimeSeconds);

            this.timeLabel.node.runAction(
                cc.repeatForever(
                    cc.sequence(
                        cc.delayTime(1),
                        cc.callFunc(() => {
                            remainingTimeSeconds--;
                            this.timeLabel!.string = TimeUtils.convertSecondsToMinutesSeconds(remainingTimeSeconds);
                            if (remainingTimeSeconds === 0) {
                                this.resetDisplayTime();
                            }
                        })
                    )
                )
            );
        }
    }
}

export default AddPowerByTime;

/**
 * 代码优化说明：
 * 1. 变量命名调整，更加见名知义：
 *    - `globalData` -> `playerGlobalData`
 *    - `currentTime` -> `currentTimeMs`
 *    - `addPowerInterval` -> `addPowerIntervalMs`
 *    - `elapsedTime` -> `elapsedAddPowerTime`
 *    - `powerToAdd` -> `powerToAddCount`
 *    - `remainingTime` -> `remainingTimeSeconds`
 * 2. 适当优化了字符串的拼接逻辑，使得代码更加易读和易于维护。
 */